package org.kfish.framework.helper;

import java.util.Set;

import org.kfish.framework.annotation.Bean;
import org.kfish.framework.bean.EmbeddedConfig;
import org.kfish.framework.config.EmbeddedConfigCustomizer;
import org.kfish.framework.log.Log;
import org.kfish.framework.log.Log.LogType;
import org.kfish.framework.util.CollectionUtil;

public class EmbeddedConfigHelper {
	private static final EmbeddedConfig embeddedConfig;
	static {
		embeddedConfig = new EmbeddedConfig();

		Set<Class<?>> BeanClassSet = ClassHelper.getClassSetBySuper(EmbeddedConfigCustomizer.class);
		try {
			if (CollectionUtil.isNotEmpty(BeanClassSet)) {

				for (Class<?> beanClass : BeanClassSet) {
					if (beanClass.isAnnotationPresent(Bean.class)) {
						EmbeddedConfigCustomizer configCustomizer = (EmbeddedConfigCustomizer) beanClass.newInstance();
						configCustomizer.customize(embeddedConfig);
					}
				}
			}
		} catch (InstantiationException | IllegalAccessException e) {

			Log.getLogger(LogType.DETAIL).error("init embedded configuration failure", e);
			throw new RuntimeException(e);

		}
	}
	
	public static EmbeddedConfig getEmbeddedConfig(){
		return embeddedConfig;
	}
}
